<?php

class Catalogue_Model_Attribute_Storage_Table extends Workset_Model_Object_Storage_Table_Abstract {

	protected $_objectClass = 'Catalogue_Model_Attribute';

    protected $_iteratorClass = 'Catalogue_Model_Object_Iterator';

	protected $_tableClass = 'Catalogue_Table_Attribute';

	protected $_attrValueTableClass = 'Catalogue_Table_Attribute_Value';

    protected $_attrValueEnumTableClass = 'Catalogue_Table_Attribute_Value_Enum';

    protected $_categoryAttrTableClass = 'Catalogue_Table_Category_Attribute';

	function setOptions(array $options) {
		parent::setOptions($options);

		foreach ($this->_options as $key => $option) {
			switch ($key) {
				case 'attrValueTableClass':
					$this->setAttrValueTableClass($option);
					unset($this->_options[$key]);
				break;

				case 'attrValueEnumTableClass':
					$this->setAttrValueEnumTableClass($option);
					unset($this->_options[$key]);
				break;

			}
		}
	}

	function setAttrValueTableClass($class) {
		$this->_attrValueTableClass = $class;
	}

	function setAttrValueEnumTableClass($class) {
		$this->_attrValueEnumTableClass = $class;
	}

	function updateValueFlags(Catalogue_Model_Attribute $attr, $flags) {
		$table = $this->_getTable($this->_attrValueTableClass);

		return $table->update(array('flag' => (int)$flags), array('attr_id = ?' => $attr->getId()));
	}

    function linkToObject(Workset_Model_Object_Interface $object,
						  Workset_Model_Object_Interface $refObject,
						  $data = null	)
    {
        $row = $object->getData();
        if ($row instanceof Workset_Db_Table_Row) {

			if (is_array($data)) {
				$data = array_merge($data, array(
					'category_id' => $refObject->getId(),
				));
			} else {
				$data = array(
					'category_id' => $refObject->getId(),
				);
			}

            return $row->addDependentRow($this->_categoryAttrTableClass, $data);
        }
    }

    function unlinkObject(Workset_Model_Object_Interface $object,
						  Workset_Model_Object_Interface $refObject)
	{
        $row = $object->getData();
        if ($row instanceof Workset_Db_Table_Row) {
            $subrow = $row->findDependentRow($this->_categoryAttrTableClass, 'attribute', array(
                'category_id = ?' => $refObject->getId(),
            ));
            if ($subrow) {
                return $subrow->delete();
            }
        }
    }


}

?>